package me.leiusual.sharing.ui.shop.adapter

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.view_holder_shop_business_apply_parent.*
import me.leiusual.sharing.R
import me.leiusual.sharing.api.model.businesscondition.PurchaseCondition

/**
 * author: leiusual
 * create on: 2019-08-02
 * description:
 */
class ShopBusinessApplyParentViewHolder(
    override val containerView: View,
    private val changeListener: () -> Unit
) : RecyclerView.ViewHolder(containerView), LayoutContainer, View.OnClickListener {

  private lateinit var purchaseCondition: PurchaseCondition

  init {
    check_box.setOnClickListener(this)
    recycler_view.adapter = ShopBusinessApplyChildAdapter(changeListener)
  }

  fun bind(purchaseCondition: PurchaseCondition) {
    this.purchaseCondition = purchaseCondition

    // 选中状态
    check_box.isChecked = purchaseCondition.productOptions.find { !it.checked!! } == null

    // 条件名称
    val storeName = purchaseCondition.storeBusinessInfo.storeName
    text_view_condition_name.text = """${storeName}（须购满￥${purchaseCondition.needConsumeAmount}元）"""

    // 商品列表
    val adapter = recycler_view.adapter as ShopBusinessApplyChildAdapter
    adapter.submitList(purchaseCondition.productOptions)
  }

  override fun onClick(v: View?) {
    when (v?.id) {
      R.id.check_box -> {
        purchaseCondition.productOptions.forEach {
          if (it.canModified) {
            it.checked = check_box.isChecked
          }
        }
        changeListener()
      }
    }
  }
}